package org.lmd.platform.sys.iml;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.lmd.platform.constant.Constant;
import org.lmd.platform.entity.sys.OrgEntity;
import org.lmd.platform.entity.sys.UserEntity;
import org.lmd.platform.shrio.ShiroUtils;
import org.lmd.platform.sys.OrgService;
import org.lmd.platform.sys.UserService;
import org.lmd.platform.util.MapUtils;
import org.lmd.platform.util.Query;
import org.lmd.platform.vo.PageUtils;
import org.lmd.platform.dao.sys.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserServiceIml extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Autowired
    private OrgService orgService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String username = (String) params.get("username");

        Page<UserEntity> page = this.selectPage(
                new Query<UserEntity>(params).getPage(),
                new EntityWrapper<UserEntity>()
                        .like(StringUtils.isNotBlank(username), "username", username)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER))
        );

        for (UserEntity UserEntity : page.getRecords()) {
            OrgEntity sysDeptEntity = orgService.selectById(UserEntity.getDeptId());
            UserEntity.setDeptName(sysDeptEntity.getName());
        }

        return new PageUtils(page);
    }

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(UserEntity user) {
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        this.baseMapper.saveUser(user);
        //角色处理
        this.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    public void update(UserEntity user) {

    }

    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        //先删除用户与角色关系
        this.baseMapper.deleteUserRole(userId);
        if (roleIdList == null || roleIdList.size() == 0) {
            return;
        }
        //保存用户与角色关系
        List<Map<String, Long>> list = new ArrayList<>(roleIdList.size());

        for (Long roleId : roleIdList) {
            Map<String, Long> userroles = new HashMap<>();
            userroles.put("userid", userId);
            userroles.put("roleid", roleId);
            list.add(userroles);
        }
        baseMapper.batchInsertUserRole(list);

    }

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        return baseMapper.queryRoleIdList(userId);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }

}
